package com.dstation.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dstation.domain.*;
import com.dstation.service.interf.IMessageService;
import com.dstation.utils.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;

import java.util.List;
import java.util.Objects;

@Controller
@Slf4j
@RequestMapping(value = "/message")
public class MessageController {

    final int pageSize=10;

    @Resource
    private IMessageService messageService;


    @RequestMapping(value = "viewMessage")
    public String viewMyMessage(@PathParam("page")Integer page, HttpSession httpSession, ModelMap modelMap){


        UserInfo userInfo=(UserInfo)httpSession.getAttribute("userInfo");


        if(userInfo==null){
            return "user/loginAndRegister";
        }
        if(Objects.isNull(page)){
            return "user/loginAndRegister";
        }

        //打开默认显示评论消息
        IPage<?> pageData=messageService.findCommentMessage(userInfo.getUserId(),new Page<>(page,pageSize));

        modelMap.put("pageData",pageData);
        modelMap.put("currentSearch", Constants.MESSAGE_COMMENT_PAGELINK);
        return "main/myMessage";
    }



    @RequestMapping(value = "commentMessage")
    public String  queryCommentMessage(@PathParam("page")Integer page, HttpSession httpSession, ModelMap modelMap){

        UserInfo userInfo=(UserInfo)httpSession.getAttribute("userInfo");

        //对不合法进入判断
        if(Objects.isNull(userInfo)){
            return "user/loginAndRegister";
        }
        if(Objects.isNull(page)){
            return "user/loginAndRegister";
        }

        IPage<?> pageData=messageService.findCommentMessage(userInfo.getUserId(),new Page<>(page,pageSize));

        modelMap.put("pageData",pageData);

        modelMap.put("currentSearch", Constants.MESSAGE_COMMENT_PAGELINK);
        return "main/myMessage";

    }

    @RequestMapping(value = "videoMessage")
    public String queryVideoMessage(@PathParam("page")Integer page, HttpSession httpSession, ModelMap modelMap){
        UserInfo userInfo=(UserInfo)httpSession.getAttribute("userInfo");


        //对不合法进入判断
        if(Objects.isNull(userInfo)){
            return "user/loginAndRegister";
        }
        if(Objects.isNull(page)){
            return "user/loginAndRegister";
        }



        IPage<?> pageData=messageService.findVideoMessage(userInfo.getUserId(),new Page<>(page,pageSize));

        modelMap.put("pageData",pageData);
        modelMap.put("currentSearch", Constants.MESSAGE_VIDEO_PAGELINK);
        return "main/myMessage";

    }
    @RequestMapping(value = "dynamicMessage")
    public String queryDynamicMessage(@PathParam("page")Integer page, HttpSession httpSession, ModelMap modelMap){


        UserInfo userInfo=(UserInfo)httpSession.getAttribute("userInfo");
        //对不合法进入判断
        if(Objects.isNull(userInfo)){
            return "user/loginAndRegister";
        }
        if(Objects.isNull(page)){
            return "user/loginAndRegister";
        }

        IPage<?> pageData=messageService.findDynamicMessage(userInfo.getUserId(),new Page<>(page,pageSize));

        modelMap.put("pageData",pageData);
        modelMap.put("currentSearch", Constants.MESSAGE_DYNAMIC_PAGELINK);

        return "main/myMessage";

    }

    @RequestMapping(value = "likedMessage")
    public String  queryLikedMessage(@PathParam("page")Integer page, HttpSession httpSession, ModelMap modelMap){


        UserInfo userInfo=(UserInfo)httpSession.getAttribute("userInfo");
        //对不合法进入判断
        if(Objects.isNull(userInfo)){
            return "user/loginAndRegister";
        }
        if(Objects.isNull(page)){
            return "user/loginAndRegister";
        }

        IPage<?> pageData=messageService.findLikedMessage(userInfo.getUserId(),new Page<>(page,pageSize));

        modelMap.put("pageData",pageData);
        modelMap.put("currentSearch", Constants.MESSAGE_LIKED_PAGELINK);

        return "main/myMessage";

    }

    @RequestMapping(value = "coinMessage")
    public String queryCoinMessage(@PathParam("page")Integer page, HttpSession httpSession, ModelMap modelMap){


        UserInfo userInfo=(UserInfo)httpSession.getAttribute("userInfo");

        //对不合法进入判断
        if(Objects.isNull(userInfo)){
            return "user/loginAndRegister";
        }
        if(Objects.isNull(page)){
            return "user/loginAndRegister";
        }
        IPage<?> pageData=messageService.findCoinMessage(userInfo.getUserId(),new Page<>(page,pageSize));

        modelMap.put("pageData",pageData);
        modelMap.put("currentSearch",Constants.MESSAGE_COIN_PAGELINK);

        return "main/myMessage";

    }

    @RequestMapping(value = "rechargeMessage")
    public String  queryRechargeMessage(@PathParam("page")Integer page, HttpSession httpSession, ModelMap modelMap){

        UserInfo userInfo=(UserInfo)httpSession.getAttribute("userInfo");
        //对不合法进入判断
        if(Objects.isNull(userInfo)){
            return "user/loginAndRegister";
        }
        if(Objects.isNull(page)){
            return "user/loginAndRegister";
        }
        IPage<?> pageData=messageService.findRechargeMessage(userInfo.getUserId(),new Page<>(page,pageSize));

        modelMap.put("pageData",pageData);
        modelMap.put("currentSearch",Constants.MESSAGE_RECHARGE_PAGELINK);

        return "main/myMessage";

    }
}
